package cn.doitedu.rtdw.udf;

import org.apache.flink.table.functions.ScalarFunction;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeTruncate extends ScalarFunction {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public String eval(String dateTimeStr,int factor) throws ParseException {
        // 2023-04-06 14:32:59
        // 60m  /  30m  /  10m
        long time = sdf.parse(dateTimeStr).getTime();
        long i = factor * 60 * 1000;
        long truncatedTime = (time/i)*i ;
        return sdf.format(new Date(truncatedTime));
    }
}
